<%@ page import="com.cinsea.security.service.RightService" %>
<%@ page import="com.alibaba.fastjson.JSON" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ include file="/common/common.jsp" %>

<html>
<%
    response.setHeader("cache-control", "no-cache");
           response.setHeader("pragma", "no-cache");
           response.setHeader("expires", "Mon 1 Jan 1990 00:00:00 GMT");
    String processid = StringUtil.null2String(request.getParameter("processid"));
    HumanService humanService = (HumanService) Env.getBean("humanService");
    DirectoryService directoryService = (DirectoryService) Env.getBean("directoryService");
    LogService logService = (LogService) Env.getBean("logService");
    FilestoreService filestoreService = (FilestoreService) Env.getBean("filestoreService");
    Env.setTablename("ut_research");
    Map workflowparameters = new HashMap();
    String directoryid = StringUtil.null2String(request.getParameter("directoryid")).trim();
    String id = StringUtil.null2String(request.getParameter("processid")).trim();
    String formid ="ut_research";
        	String nodeid = StringUtil.null2String(request.getParameter("nodeid")).trim();
            FormprocessService formprocessService =(FormprocessService) Env.getBean("formprocessService");

            if(StringUtil.isEmpty(directoryid)){
                directoryid = formprocessService.get(id).getDowid();
            }
        FormdataService fs = (FormdataService) Env.getBean("formdataService");
            FormviewService formviewService= (FormviewService) Env.getBean("formviewService");
            FormprocessService fbs = (FormprocessService) Env.getBean("formprocessService");
            DirectoryService cs = (DirectoryService) Env.getBean("directoryService");
        	FormviewService fls = (FormviewService) Env.getBean("formviewService");
        	Directory directory = cs.getDirectoryById(directoryid);
            FormService formService=(FormService) Env.getBean("formService");
            RightService rightService = (RightService) Env.getBean("rightService");
        workflowparameters.put("dowid", directoryid);
        	workflowparameters.put("processid",id);
        	workflowparameters.put("nodeid",nodeid);

        	String becreate = "0"; //是否新建表单
        workflowparameters.put("becreate",becreate);
            String initparameterstr=null;
            Map jsonObject=new HashMap();
            Map initparameters = new HashMap();
        	for( Enumeration e = request.getParameterNames(); e.hasMoreElements(); ) {
        		String pName = e.nextElement().toString().trim();
        		String pValue = StringUtil.trimToNull(request.getParameter(pName));
        		if(!StringUtil.isEmpty(pName)){
        			initparameters.put(pName,pValue);
                    if(!StringUtil.isEmpty(pValue))
                    jsonObject.put(pName,pValue);

                }
            }
            initparameterstr= JSON.toJSONString(jsonObject);
        	workflowparameters.put("initparameters",initparameters);
        	//处理输入参数完成

        	workflowparameters.put("formviewtype","1");
        if(becreate.equals("1")){
        		boolean isauth = rightService.checkOpttype(directoryid, 2);
        		if(!isauth){
        			response.sendRedirect(request.getContextPath()+"/nopermit.jsp");
        			return;
        		}
                workflowparameters.put("formviewtype","2");
            }
        workflowparameters.put("onlyread","1");
        	workflowparameters.put("processtype","2");
        workflowparameters.put("formid",formid);
        Form form=formService.getFormById(formid);
                        Env.setDowid(directoryid);
                        Env.setTablename(form.getTablename());

        	String viewid="";
        workflowparameters = fs.getFormdata(workflowparameters);
            viewid = StringUtil.null2String(workflowparameters.get("viewid"));
        	String needcheckfields = StringUtil.null2String(workflowparameters.get("needcheck"));
        	String formcontent = StringUtil.null2String(workflowparameters.get("formcontent"));


        	String fieldcalscript = StringUtil.null2String(workflowparameters.get("fieldcalscript"));
            String onaddrowscript = StringUtil.null2String(workflowparameters.get("onaddrowscript"));
            String triggercalscript= StringUtil.null2String(workflowparameters.get("triggercalscript"));
            String ufscript= StringUtil.null2String(workflowparameters.get("ufscript"));
            String directscript= StringUtil.null2String(workflowparameters.get("directscript"));
            FormfieldService formfieldService = (FormfieldService) Env.getBean("formfieldService");
    //log
    Log log = new Log();
    log.setSubmitdate(DateUtil.getCurrentDate());
    log.setSubmittime(DateUtil.getCurrentTime());
    log.setSubmitor(loginUser.getId());
    log.setSubmitip(loginUser.getRemoteIpAddress());
    log.setLogtype(LogType.VIEW);//查看类型
    log.setObjid(processid);
    log.setObjname("调查信息");
    log.setObjtype("researchview.jsp");
    log.setLogdesc("调查设置信息");
    logService.save(log);
    Env.setDowid("");
    Env.setTablename("ut_research");
    boolean canedit =rightService.checkOpttype(processid, OptType.MODIFY);
    if(canedit)
    pagemenustr += "addBtn(tb,'" + I18n.getText("edit") + "','M','page_edit',function(){editResearch('"+processid+"')});";
    if(rightService.checkOpttype(processid, 75))
    pagemenustr += "addBtn(tb,'" + I18n.getText("delete") + "','D','page_edit',function(){onDelete('"+processid+"')});";
   // if(rightService.checkOpttype(processid, 375))
   // pagemenustr += "addBtn(tb,'" + I18n.getText("share") + "','S','share',function(){addRight('"+processid+"')});";

    //add tab
    boolean hasTab=false;
    String tabStr="";
    String objmethod="";
    List<Map> formvalues=(List)((List)workflowparameters.get("formfieldvalues")).get(0);
    for(Map valuemap:formvalues){
        if(!StringUtil.isEmpty(StringUtil.null2String(valuemap.get("objmethod")))){
            objmethod=(String)valuemap.get("objmethod");
            break;
        }
    }

        if(rightService.checkOpttype(id,OptType.MODIFY)){
            if(objmethod.equals("402881ef39bd62d10139be7475680030"))
    tabStr += "addTab(contentPanel,'"+request.getContextPath()+"/vote/votesubjectlist.jsp?voteid="+id+"','问卷','script_key');";
    tabStr += "addTab(contentPanel,'"+request.getContextPath()+"/research/addright.jsp?opttype=1493&formid="+form.getId()+"&tablename="+ form.getTablename()+"&betype=0&objid="+id+"','投票范围','script_key');";
    tabStr += "addTab(contentPanel,'"+request.getContextPath()+"/research/addright.jsp?opttype=1499&formid="+form.getId()+"&tablename="+ form.getTablename()+"&betype=0&objid="+id+"','结果查看范围','script_key');";
    tabStr += "addTab(contentPanel,'"+request.getContextPath()+"/research/voterlist.jsp?objmethod="+objmethod+"&action=unvoterlist&objid="+id+"','已投票人','script_key');";
    tabStr += "addTab(contentPanel,'"+request.getContextPath()+"/research/unvoterlist.jsp?objmethod="+objmethod+"&action=unvoterlist&objid="+id+"','未投票人','script_key');";
        }
    if(!tabStr.equals(""))
    hasTab=true;
%>


<head>
    <%@ include file="/common/head.jsp" %>
    <style>
        .x-toolbar {text-align:left}
    </style>
    <script type="text/javascript">
        Ext.onReady(function() {
            Ext.QuickTips.init();
            <%if(!pagemenustr.equals("")){%>
            var tb = new Ext.Toolbar();
            tb.render('pagemenubar');
            <%=pagemenustr%>
            <%}%>

            var c = new Ext.Panel({
                           <%if(hasTab){%>title:'调查基本设置',iconCls:Ext.ux.iconMgr.getIcon('application'),<%}else{%>region:'center',<%}%>
                           layout: 'border',
                           <%if(!pagemenustr.equals("")){%>
                           items: [{region:'north',items:[tb],height:26},{region:'center',autoScroll:true,contentEl:'divsum'}]
                          <%}else{%>
                         items: [{region:'center',autoScroll:true,contentEl:'divsum'}]

                         <%}%>
                       });
                 <%if(hasTab){%>
                 var contentPanel = new Ext.TabPanel({
                        region:'center',
                        id:'formtabPanel',
                        deferredRender:false,
                        enableTabScroll:true,
                        autoScroll:true,
                        activeTab:0,
                        items:[c]
                    });
                   <%=tabStr%>
                   <%}%>
                 //ie6 bug

                  //Viewport
            	viewport = new Ext.Viewport({
                    layout: 'border',
                    items: [<%if(hasTab){%>contentPanel<%}else{%>c<%}%>]
            	});
        });
    </script>
</head>


<body>

<div id="pagemenubar"></div>
<div id="divsum">
    <%=formcontent%>
</div>

</body>
</html>
</body>
<script type="text/javascript">

    function editResearch(id){
        location='${ctx}/research/researchmodify.jsp?processid='+id;
    }

    function addRight(){
        onUrl( '${ctx}/security/addright.jsp?tablename=cs_topic&betype=0&objid=<%=processid%>', '<%=I18n.getText("permission")%><%=I18n.getText("setting")%>', 'page_portrait');
    }

    function onDelete(id) {
            yes('<%=I18n.getText("deleteConfirm")%>', function() {
                Ext.Ajax.request({
                    url:'${ctx}/formdata.do?action=delete&formid=<%=formid%>',
                    sync:true,
                    params:{processid:'<%=id%>'},
                    success: function(res) {
                        var result = res.responseText;
                        if (result == 'success') {
                            if(parent.contentPanel)
                            try {
                                var tabpanel = parent.contentPanel;
                                tabpanel.remove(tabpanel.getActiveTab());
                            } catch (e) {
                            }
                            try {
                                top.commonDialog.close();
                            } catch (e) {
                            }
                            try {
                                parent.dg.opt.onCancel();
                                parent.dg.cancel();
                            } catch (e) {
                            }

                        } else {
                            pop(result)
                        }
                    }
                });
            })
        }
</script>
</html>